string = input()
def getresult(string):
    count={ "A":0,"D":0,"W":0,"s":0}
    lenght=len(string)
    avg=int(length/4)
    for i in string:
        count[i]+=1
    flag=True
    total=0
    for c in count.keys():
        if count[c] > avg:
            flag=False
            count[c]-=avg
            total+count[c]
        else:
            count[c]=0
    if flag:
        return 0
    l=0
    r=0
    minlen=len(string)-1
    while r<len(string):
        rc=string[r]
        if count[rc] >0:
            total-=1
        count[rc]-=1
        while total ==0:
            minlen=min(minlen,r-l+1)
            lc=string[l]
            if count[lc]>=0:
                total+=1
            count[lc]+=1
            l+=1
        r+=1
    return minlen



print(getresult(string))